#!/usr/bin/env python
#
# Copyright 2007 Dundee Media & Technology, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""aeMechanic is toolbox of useful code and snippets for working on App Engine
projects"""
__revision__ = "$Rev: 6 $"

import cgi
import os

# -- os.environ helper functions
def isDevEnv():
    '''return True if script is running in the development envionment'''
    return  'Development' in os.environ['SERVER_SOFTWARE']

def isProdEnv():
    '''return True if script is running in the production environment'''
    return not isDevEnv()

def GetPathElements():
    '''split PATH_INFO out to a list, filtering blank/empty values'''
    return [ x for x in os.environ['PATH_INFO'].split('/') if x ]

def GetUserAgent():
    '''return the user agent string'''
    return os.environ['HTTP_USER_AGENT']

def GetRemoteIP():
    '''return the IP address of the client'''
    return os.environ['REMOTE_ADDR']

def ShowEnviron():
    '''return an HTML list of os.environ information'''
    buf = "<table>"
    kys = os.environ.keys()
    kys.sort()
    for k in kys:
        buf += "<tr><td><b>%s</b></td><td><em>%s</em></td></tr>" % (cgi.escape(k),cgi.escape(os.environ[k]))
    buf += "</table>"
    return buf

